﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
//using mojoPortal.Web.RS.AppCode.Lib;
using mojoPortal.Web.RS.CRM.AppCode.Lib;

namespace mojoPortal.Web.RS.CRM.Controls
{
    public partial class ClientDetailControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int Clientid = 0;

                RSUserIdentity userIdentity = GetRSUserIdentity();
                if(Request.QueryString["Clientid"] != null)
                    Clientid = Convert.ToInt32(Request.QueryString["Clientid"].ToString());
                LoadClientData(userIdentity.RSCompanyId, Clientid);

                string accountid = lblAccountID.Text;
                LoadClientOrderList(userIdentity.RSCompanyId, accountid);

                ucAttachmentControl.LoadAttachmentsByAccountID(userIdentity.RSCompanyId, accountid);

                if (Request.QueryString["selindex"] != null)
                {
                    int selectedindex = Convert.ToInt32(Request.QueryString["selindex"].ToString());
                    Accordion1.SelectedIndex = selectedindex;
                }

                if (userIdentity.RSEmployeeId.ToString() != lblAgentID.Text)
                {
                    btnManageClientOrder.Visible = false;
                    btnSaveAccountInfo.Visible = false;
                    btnSaveAddressInfo.Visible = false;
                    btnSaveClientInfo.Visible = false;
                    btnSaveDescription.Visible = false;
                    btnSaveNotes.Visible = false;
                }

                //Begin Activity History
                LoadActivityCode(userIdentity.RSCompanyId);
                //End Activity History
            }
        }

        protected void btnSaveDescription_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE [dbo].[rs_tblClient] SET " +
                    "[DescriptionInfo] = @DescriptionInfo, " +
                    "[Audit_UpdateBy] = @CurrentUser, " +
                    "[Audit_UpdateDt] = GETDATE() " +
                    "WHERE CompanyID=@CompanyId AND ClientID=@ClientId";

            RSUserIdentity userIdentity = GetRSUserIdentity();
            int CompanyId = userIdentity.RSCompanyId;
            int ClientId = lblClientID.Text == "" ? 0 : Convert.ToInt32(lblClientID.Text);
            string descriptoninfo = txtDescription.Text;

            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@DescriptionInfo", SqlDbType.NVarChar, -1, ParameterDirection.Input, descriptoninfo);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ClientId", SqlDbType.Int, 0, ParameterDirection.Input, ClientId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", "3");
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Description Information Successfully Saved!", url, true);
        }
        protected void btnSaveNotes_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE [dbo].[rs_tblClient] SET " +
                    "[Note] = @note, " +
                    "[Audit_UpdateBy] = @CurrentUser, " +
                    "[Audit_UpdateDt] = GETDATE() " +
                    "WHERE CompanyID=@CompanyId AND ClientID=@ClientId";

            RSUserIdentity userIdentity = GetRSUserIdentity();
            int CompanyId = userIdentity.RSCompanyId;
            int ClientId = lblClientID.Text == "" ? 0 : Convert.ToInt32(lblClientID.Text);
            string note = txtNote.Text;

            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Note", SqlDbType.NVarChar, -1, ParameterDirection.Input, note);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ClientId", SqlDbType.Int, 0, ParameterDirection.Input, ClientId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", "4");
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Notes Successfully Saved!", url, true);
        }
        
        protected void btnManageClientOrder_Click(object sender, EventArgs e)
        {
            string accountid = lblAccountID.Text;
            RSUserIdentity userIdentity = GetRSUserIdentity(); //new RSUserIdentity();
            ucModalAccountOrderControl.ShowModal(userIdentity.RSCompanyId, accountid);
        }

        protected void btnDeleteClient_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int Clientid = lblClientID.Text == "" ? 0 : Convert.ToInt32(lblClientID.Text);

                if (companyid > 0 && Clientid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@Clientid", SqlDbType.Int, 0, ParameterDirection.Input, Clientid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblClient WHERE ClientId=@Clientid AND CompanyId=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Client Deleted Successfully!", "RSViewClientByAgent.aspx", true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void btnSaveClientInfo_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE [dbo].[rs_tblClient] SET " +
                    "[Phone] = @Phone, " +
                    "[Fax] = @Fax, " +
                    "[Email] = @Email, " +
                    "[Mobile] = @Mobile, " +
                    "[Title] = @Title, " +
                    "[FirstName] = @FirstName, " +
                    "[LastName] = @LastName, " +
                    "[MiddleName] = @MiddleName, " +
                    "[Audit_UpdateBy] = @CurrentUser, " +
                    "[Audit_UpdateDt] = GETDATE() " +
                    "WHERE CompanyID=@CompanyId AND ClientID=@ClientId";

            RSUserIdentity userIdentity = GetRSUserIdentity();
            int CompanyId = userIdentity.RSCompanyId;
            int ClientId = lblClientID.Text == "" ? 0 : Convert.ToInt32(lblClientID.Text);
            string FirstName = txtFirstName.Text.Trim();
            string MiddleName = txtMiddleName.Text.Trim();
            string LastName = txtLastName.Text.Trim();
            string Phone = txtPhone.Text.Trim();
            string Mobile = txtMobile.Text.Trim();
            string Fax = txtFax.Text.Trim();
            string Email = txtEmail.Text.Trim();
            string Title = txtTitle.Text.Trim();
            SqlCommand sqlCmd = new SqlCommand();

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Phone", SqlDbType.NVarChar, 15, ParameterDirection.Input, Phone);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Fax", SqlDbType.NVarChar, 15, ParameterDirection.Input, Fax);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 50, ParameterDirection.Input, Email);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Mobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, Mobile);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Title", SqlDbType.NVarChar, 25, ParameterDirection.Input, Title);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@FirstName", SqlDbType.NVarChar, 25, ParameterDirection.Input, FirstName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleName", SqlDbType.NVarChar, 25, ParameterDirection.Input, MiddleName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@LastName", SqlDbType.NVarChar, 25, ParameterDirection.Input, LastName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ClientId", SqlDbType.Int, 0, ParameterDirection.Input, ClientId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", "0");
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Client Information Successfully Saved!", url, true);
        }
        protected void btnSaveAccountInfo_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE [dbo].[rs_tblClient] SET " +
                        "AccountOwner = @AccountOwner, " +
                        "InsuredAnnuitantName = @InsuredAnnuitantName, " +
                        "AccountNumber = @AccountNumber, " +
                        "AccountType = @AccountType, " +
                        "PersonalFinancialStrategy = @PersonalFinancialStrategy, " +
                        "AnnualStatement = @AnnualStatement, " +
                        "AccountStatus = @AccountStatus, " +
                        "AnnualIncome = @AnnualIncome, " +
                        "AnnualReviewMonth = @AnnualReviewMonth, " +
                        "AccountNumberField = @AccountNumberField, " +
		                "Audit_UpdateBy = @CurrentUser, " +
    		            "Audit_UpdateDt = GETDATE() " +
                        "WHERE CompanyID=@CompanyId AND ClientID=@ClientId";

            string AccountOwner = txtAccountOwner.Text.Trim();
            string InsuredAnnuitantName = txtInsuredAnnuitantName.Text.Trim();
            string AccountNumber = txtAccountNumber.Text.Trim();
            string AccountType = txtAccountType.Text.Trim();
            string PersonalFinancialStrategy = txtPersonalFinancialStrategy.Text.Trim();
            string AnnualStatement = txtAnnualStatement.Text.Trim();
            int AccountStatus = int.Parse(ddlAccountStatus.SelectedValue.ToString());
            int AnnualIncome = txtAnnualIncome.Text == "" ? 0 : int.Parse(txtAnnualIncome.Text);
            string AnnualReviewMonth = ddlAnnualReviewMonth.SelectedItem.Text;
            string AccountNumberField = txtAccountNumberField.Text.Trim();
            int ClientId = lblClientID.Text == "" ? 0 : Convert.ToInt32(lblClientID.Text);

            RSUserIdentity userIdentity = GetRSUserIdentity();
            SqlCommand sqlCmd = new SqlCommand();

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountOwner", SqlDbType.NVarChar, 15, ParameterDirection.Input, AccountOwner);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@InsuredAnnuitantName", SqlDbType.NVarChar, 50, ParameterDirection.Input, InsuredAnnuitantName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountNumber", SqlDbType.NVarChar, 15, ParameterDirection.Input, AccountNumber);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountType", SqlDbType.NVarChar, 20, ParameterDirection.Input, AccountType);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@PersonalFinancialStrategy", SqlDbType.NVarChar, 250, ParameterDirection.Input, PersonalFinancialStrategy);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AnnualStatement", SqlDbType.NVarChar, 50, ParameterDirection.Input, AnnualStatement);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountStatus", SqlDbType.Int, 0, ParameterDirection.Input, AccountStatus);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AnnualIncome", SqlDbType.Int, 0, ParameterDirection.Input, AnnualIncome);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AnnualReviewMonth", SqlDbType.NVarChar, 20, ParameterDirection.Input, AnnualReviewMonth);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountNumberField", SqlDbType.NVarChar, 15, ParameterDirection.Input, AccountNumberField);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ClientId", SqlDbType.Int, 0, ParameterDirection.Input, ClientId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", "1");
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Account Information Successfully Saved!", url, true);
        }
        protected void btnSaveAddressInfo_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE [dbo].[rs_tblClient] SET " +
            "BillingStreet = @BillingStreet, " +
            "ShippingStreet = @ShippingStreet, " +
            "BillingCity = @BillingCity, " +
            "ShippingCity = @ShippingCity, " +
            "BillingState = @BillingState, " +
            "ShippingState = @ShippingState, " +
            "BillingZipCode = @BillingZipCode, " +
            "ShippingZipCode = @ShippingZipCode, " +
            "BillingCountry = @BillingCountry, " +
            "ShippingCountry = @ShippingCountry, " +
		    "Audit_UpdateBy = @CurrentUser, " +
		    "Audit_UpdateDt = GETDATE() " +
            "WHERE CompanyID=@CompanyId AND ClientID=@ClientId";

            string BillingStreet = txtBillingStreet.Text.Trim();
            string ShippingStreet = txtShippingStreet.Text.Trim();
            string BillingCity = txtBillingCity.Text.Trim();
            string ShippingCity = txtShippingCity.Text.Trim();
            string BillingState = txtBillingState.Text.Trim();
            string ShippingState = txtShippingState.Text.Trim();
            string BillingZipCode = txtBillingZipCode.Text.Trim();
            string ShippingZipCode = txtShippingZipCode.Text.Trim();
            string BillingCountry = txtBillingCountry.Text.Trim();
            string ShippingCountry = txtShippingCountry.Text.Trim();
            int ClientId = lblClientID.Text == "" ? 0 : Convert.ToInt32(lblClientID.Text);

            RSUserIdentity userIdentity = GetRSUserIdentity();
            SqlCommand sqlCmd = new SqlCommand();

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BillingStreet", SqlDbType.NVarChar, 100, ParameterDirection.Input, BillingStreet);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ShippingStreet", SqlDbType.NVarChar, 100, ParameterDirection.Input, ShippingStreet);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BillingCity", SqlDbType.NVarChar, 50, ParameterDirection.Input, BillingCity);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ShippingCity", SqlDbType.NVarChar, 50, ParameterDirection.Input, ShippingCity);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BillingState", SqlDbType.NVarChar, 30, ParameterDirection.Input, BillingState);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ShippingState", SqlDbType.NVarChar, 30, ParameterDirection.Input, ShippingState);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BillingZipCode", SqlDbType.NVarChar, 15, ParameterDirection.Input, BillingZipCode);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ShippingZipCode", SqlDbType.NVarChar, 15, ParameterDirection.Input, ShippingZipCode);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BillingCountry", SqlDbType.NVarChar, 50, ParameterDirection.Input, BillingCountry);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ShippingCountry", SqlDbType.NVarChar, 50, ParameterDirection.Input, ShippingCountry);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ClientId", SqlDbType.Int, 0, ParameterDirection.Input, ClientId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", "2");
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Address Information Successfully Saved!", url, true);

        }
        public bool LoadClientData(int companyid, int Clientid)
        {
            bool bHasValue = false;
            RSCRMDataLookupLoader.LoadMonthDDL(ddlAnnualReviewMonth);
            RSCRMDataLookupLoader.LoadStatusDDL(companyid, "ACCOUNT_CAT", ddlAccountStatus);

            if (companyid > 0 && Clientid > 0)
            {
                String strSQL = "select * from rs_tblClient where Clientid=@Clientid and companyid=@companyid";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(strSQL, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@Clientid";
                param.Value = Clientid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();

                    //Header Info
                    int agentid = Convert.ToInt32(myDataReader["AgentID"].ToString());
                    lblAgentID.Text = agentid.ToString();
                    lblAgentName.Text = RSCRMCommonFunctions.GetEmployeeNameByID(companyid, agentid);
                    lblAccountOwner.Text = myDataReader["AccountOwner"].ToString();
                    lblClientID.Text = Clientid.ToString();
                    lblAccountID.Text = myDataReader["AccountID"].ToString();
                    lblClientName.Text = myDataReader["FirstName"].ToString() + " " + myDataReader["LastName"].ToString();
                    lblEmployees.Text = myDataReader["NumEmployees"].ToString();
                    lblAnnualIncome.Text = myDataReader["AnnualIncome"].ToString();
                    lblCreatedBy.Text = myDataReader["Audit_CreateBy"].ToString();
                    if (myDataReader["Audit_CreateDt"] != DBNull.Value)
                    {
                        lblCreateDt.Text = myDataReader["Audit_CreateDt"].ToString();
                    }

                    //Client Info
                    txtFirstName.Text = myDataReader["FirstName"].ToString();
                    txtLastName.Text = myDataReader["LastName"].ToString();
                    txtPhone.Text = myDataReader["Phone"].ToString();
                    txtMobile.Text = myDataReader["Mobile"].ToString();
                    txtFax.Text = myDataReader["Fax"].ToString();
                    txtEmail.Text = myDataReader["Email"].ToString();
                    txtTitle.Text = myDataReader["Title"].ToString();
                    txtMiddleName.Text = myDataReader["MiddleName"].ToString();

                    //Account Info
                    ddlAccountStatus.SelectedValue = myDataReader["AccountStatus"].ToString();
                    txtAccountOwner.Text = myDataReader["AccountOwner"].ToString();
                    txtInsuredAnnuitantName.Text = myDataReader["InsuredAnnuitantName"].ToString();
                    txtAccountNumber.Text = myDataReader["AccountNumber"].ToString();
                    txtAccountType.Text = myDataReader["AccountType"].ToString();
                    txtPersonalFinancialStrategy.Text = myDataReader["PersonalFinancialStrategy"].ToString();
                    txtAnnualStatement.Text = myDataReader["AnnualStatement"].ToString();
                    txtAccountNumberField.Text = myDataReader["AccountNumberField"].ToString();
                    txtAnnualIncome.Text = myDataReader["AnnualIncome"].ToString();

                    if (myDataReader["AccountStatus"] != DBNull.Value)
                    {
                        ddlAccountStatus.SelectedValue = myDataReader["AccountStatus"].ToString();
                    }
                    if (myDataReader["AnnualReviewMonth"] != DBNull.Value)
                    {
                        ddlAnnualReviewMonth.SelectedValue = myDataReader["AnnualReviewMonth"].ToString();
                    }
                    //if (myDataReader["DeliveryReceipt"] != DBNull.Value)
                    //{
                    //    DateTime deliveryreceiptdt = Convert.ToDateTime(myDataReader["DeliveryReceipt"].ToString());
                    //    txtDeliveryReceipt.Text = deliveryreceiptdt.ToShortDateString();
                    //}
                    //if (myDataReader["ApplicationDate"] != DBNull.Value)
                    //{
                    //    DateTime applicationdt = Convert.ToDateTime(myDataReader["ApplicationDate"].ToString());
                    //    txtApplicationDate.Text = applicationdt.ToShortDateString();
                    //}
                    //if (myDataReader["IssueDate"] != DBNull.Value)
                    //{
                    //    DateTime issuedt = Convert.ToDateTime(myDataReader["IssueDate"].ToString());
                    //    txtIssueDate.Text = issuedt.ToShortDateString();
                    //}


                    //Addrress Info
                    txtBillingStreet.Text = myDataReader["BillingStreet"].ToString();
                    txtShippingStreet.Text = myDataReader["ShippingStreet"].ToString();
                    txtBillingCity.Text = myDataReader["BillingCity"].ToString();
                    txtShippingCity.Text = myDataReader["ShippingCity"].ToString();
                    txtBillingState.Text = myDataReader["BillingState"].ToString();
                    txtShippingState.Text = myDataReader["ShippingState"].ToString();
                    txtBillingZipCode.Text = myDataReader["BillingZipCode"].ToString();
                    txtShippingZipCode.Text = myDataReader["ShippingZipCode"].ToString();
                    txtBillingCountry.Text = myDataReader["BillingCountry"].ToString();
                    txtShippingCountry.Text = myDataReader["ShippingCountry"].ToString();

                    txtDescription.Text = myDataReader["DescriptionInfo"].ToString();
                    txtNote.Text = myDataReader["Note"].ToString();

                    bHasValue = true;
                }

                myConnection.Close();
            }
            return bHasValue;
        }

        public bool LoadClientOrderList(int companyid, string accountid)
        {
            bool bHasValue = false;
            if (companyid > 0 && accountid != null)
            {
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                String sqlSELECT = "SELECT c.AccountOrderID, c.AccountID, c.ProductID, c.ContractNum, ISNULL(c.IsQualified, '') as IsQualified, c.DeliveryDate, c.ApplicationDate, c.IssueDate, d.productname, v.providername FROM [dbo].[rs_tblAccountOrder] c INNER JOIN rs_tblProduct d ON c.productid=d.productid INNER JOIN rs_tblProvider v ON d.providerid=v.providerid WHERE c.AccountID=@accountid AND c.CompanyID=@companyid";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@accountid";
                param.Value = accountid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvClientOrder.DataSource = ds;
                gvClientOrder.DataBind();
                myConnection.Close();

                if (gvClientOrder.Rows.Count > 0)
                {
                    bHasValue = true;
                }
            }
            return bHasValue;
        }

        //Begin Activity History
        private void LoadActivityCodeDDL(int companyid)
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "SELECT * FROM rs_tblActivityCode WHERE CompanyId=@companyid ORDER BY ActivityCode ASC";

            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            SqlParameter param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlActivityCode.Items.Clear();
            ddlActivityCode.DataSource = myDataReader;
            ddlActivityCode.DataTextField = "ActivityCode";
            ddlActivityCode.DataValueField = "ActivityCodeID";
            ddlActivityCode.DataBind();
            ddlActivityCode.Items.Insert(0, new ListItem("< Please select >", "0"));
            myConnection.Close();
        }
        protected void ddlActivityCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlActivityCode.SelectedValue != "0")
            {
                if (ddlActivityCode.SelectedItem.Text == "Marital Status")
                {
                    ddlMaritalStatus.Visible = true;
                    ddlMaritalStatus.SelectedIndex = 0;
                    txtActivityData.Visible = false;
                    btnAddActivityHistory.Enabled = true;

                    RequiredField_ActivityData.ControlToValidate = "ddlMaritalStatus";
                }
                else
                {
                    txtActivityData.Visible = true;
                    txtActivityData.BackColor = System.Drawing.Color.White;
                    txtActivityData.ReadOnly = false;
                    btnAddActivityHistory.Enabled = true;
                    ddlMaritalStatus.Visible = false;

                    RequiredField_ActivityData.ControlToValidate = "txtActivityData";
                }
                //RSUserIdentity userIdentity = GetRSUserIdentity();
                //LoadActivityCode(userIdentity.RSCompanyId);
            }
            else
            {
                ddlMaritalStatus.Visible = false;
                txtActivityData.Visible = true;
                txtActivityData.BackColor = System.Drawing.Color.Gainsboro;
                txtActivityData.ReadOnly = true;
                btnAddActivityHistory.Enabled = false;
            }
        }
        private bool LoadActivityCode(int companyid)
        {
            bool bHasValue = false;
            if (companyid > 0)
            {
                LoadActivityCodeDDL(companyid);
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                String sqlSELECT = "SELECT * FROM rs_tblActivityCode WHERE CompanyId=@companyid ORDER BY ActivityCode ASC";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvActivityCode.DataSource = ds;
                gvActivityCode.DataBind();
                myConnection.Close();

                if (gvActivityCode.Rows.Count > 0)
                {
                    bHasValue = true;
                }
            }
            return bHasValue;
        }

        private DataSet GetChildListDataSet(int companyid, string accountid, int activitycodeid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select h.activityhistoryid, h.history, h.audit_createdt, c.activitycode from rs_tblActivityHistory h inner join rs_tblActivityCode c " +
                                    "on h.ActivityCodeID=c.ActivityCodeID where h.activitycodeid=@activitycodeid and h.AccountID=@accountid and h.CompanyID=@companyid order by h.audit_createdt desc";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.NVarChar;
            param.ParameterName = "@accountid";
            param.Value = accountid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@activitycodeid";
            param.Value = activitycodeid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        protected void gvActivityCode_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = (GridView)e.Row.FindControl("gvActivityHistory");
                if (null != gv && gv is GridView)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    int activitycodeid = Convert.ToInt32(drv["ActivityCodeID"]);
                    gv.DataSource = GetChildListDataSet(companyid, lblAccountID.Text, activitycodeid);
                    gv.DataBind();
                }
            }
        }
        protected void btnAddActivityHistory_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                string accountid = lblAccountID.Text;
                int activitycodeid = int.Parse(ddlActivityCode.SelectedValue);
                string activitydata = string.Empty;
                if (ddlActivityCode.SelectedItem.Text == "Marital Status")
                {
                    activitydata = ddlMaritalStatus.SelectedValue;
                }
                else
                {
                    activitydata = txtActivityData.Text;
                }

                if (activitydata.Length > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountID", SqlDbType.NVarChar, 15, ParameterDirection.Input, accountid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityCodeID", SqlDbType.Int, 0, ParameterDirection.Input, activitycodeid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ActivityData", SqlDbType.NVarChar, 250, ParameterDirection.Input, activitydata);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 15, ParameterDirection.Input, userIdentity.RSCurrentUser);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveActivityHistory");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);

                    var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
                    nameValues.Set("selindex", "6");
                    string url = Request.Url.AbsolutePath;
                    string updatedQueryString = "?" + nameValues.ToString();
                    url = url + updatedQueryString;
                    if (returnValue > 0)
                        ucModalActionMessageControl.ShowModal("Activity History Added Successfully!", url, true);
                    else
                        ucModalActionMessageControl.ShowModal("Failed To Add Activity History.  Please Try Again.", "", false);
                }
                else
                    ucModalActionMessageControl.ShowModal("Failed To Add Activity History.  Please Try Again.", "", false);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        //End of ActivityHistory
    }
}